﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using JobBoardAdminPanel.Helpers;
using JobBoardAdminPanel.Commands;

namespace JobBoardAdminPanel.ViewModels
{
    public class EditOffersViewModel : ViewModelBase
    {
        private List<StatusViewModel> statuses = new List<StatusViewModel>(ViewModelsHelper.GetAllStatuses());
        private StatusViewModel currentStatus;
        private List<IndustryViewModel> industries = new List<IndustryViewModel>(ViewModelsHelper.GetAllIndustries());
        private IndustryViewModel currentIndustry;
        private List<CompanyViewModel> companies = new List<CompanyViewModel>(ViewModelsHelper.GetAllCompanies());
        private CompanyViewModel currentCompany;
        private List<CountryViewModel> countries = new List<CountryViewModel>(ViewModelsHelper.GetAllCountries());
        private CountryViewModel currentCountry;
        private List<CityViewModel> cities = new List<CityViewModel>(ViewModelsHelper.GetAllCities());
        private CityViewModel currentCity;
        private string jobTitle;
        private string jobDescription;
        private ObservableCollection<OfferViewModel> offers;
        private OfferViewModel currentOffer;
        private OfferViewModel newOffer;
        private SaveOfferCommand saveCommand;
        private DeleteOfferCommand deleteCommand;
        private AddOfferCommand addCommand;

        public EditOffersViewModel()
        {
            this.saveCommand = new SaveOfferCommand(this);
            this.addCommand = new AddOfferCommand(this);
            this.deleteCommand = new DeleteOfferCommand(this);
            NewOffer = new OfferViewModel(new JobsBoardDAL.DTOs.JobOfferDTO());
            CurrentOffer = new OfferViewModel(new JobsBoardDAL.DTOs.JobOfferDTO());
            RefreshOffers();
        }
  
        private void RefreshOffers()
        {
            Offers = new ObservableCollection<OfferViewModel>(ViewModelsHelper.GetAllOffers());
        }

        public OfferViewModel NewOffer
        {
            get
            {
                return newOffer;
            }
            set
            {
                if (this.newOffer != value)
                {
                    newOffer = value;
                    OnPropertyChanged("NewOffer");
                }
            }
        }

        public SaveOfferCommand SaveCommand
        {
            get
            {
                return saveCommand;
            }
            set
            {
                saveCommand = value;
            }
        }

        public DeleteOfferCommand DeleteCommand
        {
            get
            {
                return deleteCommand;
            }
            set
            {
                deleteCommand = value;
            }
        }

        public AddOfferCommand AddCommand
        {
            get
            {
                return addCommand;
            }
            set
            {
                addCommand = value;
            }
        }

        public OfferViewModel CurrentOffer
        {
            get
            {
                return currentOffer;
            }
            set
            {
                if (this.currentOffer != value)
                {
                    currentOffer = value;
                    this.Cities = new List<CityViewModel>(ViewModelsHelper.GetAllCities());
                    MarkCurrents();
                    OnPropertyChanged("CurrentOffer");
                }
            }
        }

        private void MarkCurrents()
        {
            if (CurrentOffer == null)
            {
                CurrentOffer = new OfferViewModel(new JobsBoardDAL.DTOs.JobOfferDTO());
            }

            foreach (StatusViewModel status in Statuses)
            {
                

                if (status.StatusName == currentOffer.Status)
                {
                    CurrentStatus = status;
                }
            }

            foreach (IndustryViewModel industry in Industries)
            {
                if (industry.IndustryName == currentOffer.Industry.IndustryName)
                {
                    CurrentIndustry = industry;
                }
            }

            foreach (CompanyViewModel company in Companies)
            {
                if (company.Name == currentOffer.Company.Name)
                {
                    CurrentCompany = company;
                }
            }

            foreach (CountryViewModel country in Countries)
            {
                if (country.CountryName == currentOffer.Country)
                {
                    CurrentCountry = country;
                }
            }

            foreach (CityViewModel city in Cities)
            {
                if (city.CityName == currentOffer.City)
                {
                    CurrentCity = city;
                }
            }
        }

        public string JobDescription
        {
            get
            {
                return jobDescription;
            }
            set
            {
                if (this.jobDescription != value)
                {
                    jobDescription = value;
                    OnPropertyChanged("JobDescription");
                    CurrentOffer.JobDescription = jobDescription;
                }
            }
        }

        public IndustryViewModel CurrentIndustry
        {
            get
            {
                return currentIndustry;
            }
            set
            {
                currentIndustry = value;
                OnPropertyChanged("CurrentIndustry");
                CurrentOffer.Industry = currentIndustry;
                NewOffer.Industry = currentIndustry;
            }
        }

        public CompanyViewModel CurrentCompany
        {
            get
            {
                return currentCompany;
            }
            set
            {
                currentCompany = value;
                OnPropertyChanged("CurrentCompany");
                CurrentOffer.Company = CurrentCompany;
                NewOffer.Company = CurrentCompany;
            }
        }

        public StatusViewModel CurrentStatus
        {
            get
            {
                return currentStatus;
            }
            set
            {
                currentStatus = value;
                this.CurrentOffer.Status = currentStatus.StatusName;
                OnPropertyChanged("CurrentStatus");
                NewOffer.Status = currentStatus.StatusName;
            }
        }

        public List<IndustryViewModel> Industries
        {
            get
            {
                return industries;
            }
            set
            {
                industries = value;
                OnPropertyChanged("Industries");
            }
        }

        public List<CompanyViewModel> Companies
        {
            get
            {
                return companies;
            }
            set
            {
                companies = value;
                OnPropertyChanged("Companies");
            }
        }

        public List<StatusViewModel> Statuses
        {
            get
            {
                return statuses;
            }
            set
            {
                statuses = value;
                OnPropertyChanged("Statuses");
            }
        }

        public List<CountryViewModel> Countries
        {
            get
            {
                return countries;
            }
            set
            {
                countries = value;
                OnPropertyChanged("Country");
            }
        }

        public List<CityViewModel> Cities
        {
            get
            {
                return cities;
            }
            set
            {
                cities = value;
                OnPropertyChanged("Cities");
            }
        }

        public CountryViewModel CurrentCountry
        {
            get
            {
                return currentCountry;
            }
            set
            {
                currentCountry = value;
                Cities = new List<CityViewModel>(ViewModelsHelper.GetCitiesByCountry(CurrentCountry));
                OnPropertyChanged("CurrentCountry");
                NewOffer.Country = CurrentCountry.CountryName;
            }
        }

        public CityViewModel CurrentCity
        {
            get
            {
                return currentCity;
            }
            set
            {
                currentCity = value;
                OnPropertyChanged("CurrentCity");
                if (value != null)
                {
                    NewOffer.City = value.CityName;
                }
            }
        }

        public ObservableCollection<OfferViewModel> Offers
        {
            get
            {
                return offers;
            }
            set
            {
                offers = value;
                OnPropertyChanged("Offers");
            }
        }
        
        public string JobTitle
        {
            get
            {
                return jobTitle;
            }
            set
            {
                jobTitle = value;
                OnPropertyChanged("JobTitle");
                CurrentOffer.JobTitle = jobTitle;
            }
        }

        internal void Add()
        {
            this.NewOffer.Add();
            RefreshOffers();
        }

        internal void Delete()
        {
            this.CurrentOffer.Delete();
            RefreshOffers();
        }

        internal void Update()
        {
            this.CurrentOffer.Update();
            RefreshOffers();
        }
    }
}